package net.shopxx.action.admin;

import java.util.List;
import java.util.Set;

import net.shopxx.entity.Menu;
import net.shopxx.service.MenuService;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - 菜单
 */

@ParentPackage("admin")
public class MenuAction extends BaseAdminAction {

/**
	 * 
	 */
	private static final long serialVersionUID = -2790960017720375751L;
	private String parentId;
	private Menu menu;
	private List<Menu> menuTreeList;
	
	@javax.annotation.Resource
	private MenuService menuService;
	
	// 商店配置
	public String setting() {
		return "setting";
	}
	
	// 会员
	public String member() {
		return "member";
	}
	
	// 商品
	public String product() {
		return "product";
	}
	
	// 生产
	public String produce() {
		return "produce";
	}
	// 物流
	public String delivery() {
		return "delivery";
	}
	
	// 页面管理
	public String content() {
		return "content";
	}
	
	// 订单管理
	public String order() {
		return "order";
	}
	
	// 管理员
	public String admin() {
		return "admin";
	}

	// 主体
	public String main() {
		return "main";
	}

	// 常用
	public String common() {
		return "common";
	}

	// 头部
	public String header() {
		return "header";
	}
	
	// 列表
	public String list() {
		pager = menuService.findByPager(pager);
		return LIST;
	}

	// 删除
	public String delete() throws Exception{
		Menu menu = menuService.load(id);
		Set<Menu> childrenMenuSet = menu.getChildren();
		redirectionUrl = "menu!list.action";
		if (childrenMenuSet != null && childrenMenuSet.size() > 0) {
			addActionError("此菜单存在下级菜单，删除失败!");
			return ERROR;
		}
		menuService.delete(id);
//		return ajaxJsonSuccessMessage("删除成功！");
		return SUCCESS;
	}

	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		menu = menuService.load(id);
		return INPUT;
	}

	// 保存
	@Validations(
			requiredStrings = { 
					@RequiredStringValidator(fieldName = "menu.name", message = "菜单名称不允许为空!"),
				}, 
				requiredFields = { 
					@RequiredFieldValidator(fieldName = "menu.orderList", message = "排序不允许为空!")
				},
				intRangeFields = {
					@IntRangeFieldValidator(fieldName = "menu.orderList", min = "0", message = "排序必须为零或正整数!")
				}
	)
	@InputConfig(resultName = "error")
	public String save() throws Exception {
		if (StringUtils.isNotEmpty(parentId)) {
			Menu parent = menuService.load(parentId);
			menu.setParent(parent);
		} else {
			menu.setParent(null);
		}
		menuService.save(menu);
		redirectionUrl = "menu!list.action";
		return SUCCESS;
	}

	// 更新
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "menu.name", message = "菜单名称不允许为空!"),
		}, 
		requiredFields = { 
				@RequiredFieldValidator(fieldName = "menu.orderList", message = "排序不允许为空!")
			},
			intRangeFields = {
				@IntRangeFieldValidator(fieldName = "menu.orderList", min = "0", message = "排序必须为零或正整数!")
			}
	)
	@InputConfig(resultName = "error")
	public String update() throws Exception {
		Menu persistent = menuService.load(id);
		BeanUtils.copyProperties(menu, persistent, new String[]{"id", "createDate", "modifyDate", "path", "parent", "children"});
		menuService.update(persistent);
		redirectionUrl = "menu!list.action";
		return SUCCESS;
	}

	/**
	 * @return the parentId
	 */
	public String getParentId() {
		return parentId;
	}

	/**
	 * @param parentId the parentId to set
	 */
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	/**
	 * @return the menu
	 */
	public Menu getMenu() {
		return menu;
	}

	/**
	 * @param menu the menu to set
	 */
	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	/**
	 * @return the menuTreeList
	 */
	public List<Menu> getMenuTreeList() {
		menuTreeList = menuService.getMenuTreeList();
		return menuTreeList;
	}

	/**
	 * @param menuTreeList the menuTreeList to set
	 */
	public void setMenuTreeList(List<Menu> menuTreeList) {
		this.menuTreeList = menuTreeList;
	}

	/**
	 * @return the menuService
	 */
	public MenuService getMenuService() {
		return menuService;
	}

	/**
	 * @param menuService the menuService to set
	 */
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
	
}